package leetcode;

/**
 * @Description 罗马数字转整数
 * @Author away
 * @Version V1.0.0
 * @Since 1.0
 * @Date 21.5.15
 */

public class T13 {

    public static void main(String[] args) {
        T13 t13 = new T13();
        String s = "III";
        System.out.println(t13.romanToInt(s));
    }

    String[] romCodes = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};

    public int romanToInt(String s) {
        int result = 0;
        while (s.length() > 0) {
            for (int i = 0; i < romCodes.length; i++) {
                if (s.startsWith(romCodes[i])) {
                    result += values[i];
                    s = s.substring(romCodes[i].length());
                    break;
                }
            }
        }
        return result;
    }
}
